package com.yanqu.road.server.manger.activity.curio;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.enums.eTaskType;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.TaskInfo;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.HideGoodsMgr;
import com.yanqu.road.server.manger.config.TaskMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CurioConfigMgr extends TempMgr {

    private static CurioConfig config;

    //缓存下藏品屏蔽信息
    private static List<Integer> lastHideGoods = new ArrayList<>();

    @Override
    public boolean init() throws Exception {
        lastHideGoods = getHideGoods();
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        CurioConfig cc = new CurioConfig();
        boolean result = cc.init();
        config = cc;
        return result;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static void reloadCurioConfig(){
        List<Integer> nowHideList = getHideGoods();
        if(!nowHideList.equals(lastHideGoods)){
            CurioConfig cc = new CurioConfig();
            boolean result = cc.init();
            if(result) {
                config = cc;
                lastHideGoods = nowHideList;
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }


    public static List<Integer> getHideGoods(){
        List<Integer> itemList = new ArrayList<>();
        for (Integer hideGood : HideGoodsMgr.getHideGoods()) {
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(hideGood);
            if(goodsInfo != null && goodsInfo.getType() == eGoodsType.CurioItemExchange.getValue()){
                int itemId = goodsInfo.getParamList().get(0).intValue();
                itemList.add(itemId);
            }
        }
        return itemList;
    }

    public static CurioConfig getConfig() {
        return config;
    }

    public static List<Integer> getConditionList(int itemId){
        List<Integer> list = new ArrayList<>();
        for (Map.Entry<Integer, TaskInfo> entry : TaskMgr.getTaskMap().entrySet()) {
            TaskInfo taskInfo = entry.getValue();
            if(taskInfo.getType() == eTaskType.CurioScoreTask.getValue()){
                TaskConditionInfo condition = TaskMgr.getTaskConditionById(taskInfo.getTaskId());
                if(condition != null && itemId == condition.getParamList().get(2).intValue()){
                    list.add(taskInfo.getTaskId());
                }
            }
        }
        return list;
    }
}
